﻿#region Header
//************************************************************************************
// Name: WebUtils
// Description: WebUtils
// Created On:  02-Aug-2011
// Created By:  Swathi
// Last Modified On:
// Last Modified By:
// Last Modified Reason:
//*************************************************************************************
#endregion Header
using System.Collections;
using System.Configuration;
using System.Text.RegularExpressions;
using System.Web;
using Spring.Core;
using Spring.Expressions;
using Spring.Core.IO;
using Spring.Context;
using Spring.Context.Support;
using System.IO;

namespace Base.Utils
{
    public class WebUtils
    {
        private static readonly Regex MobileRegex =
            new Regex(ConfigurationManager.AppSettings.Get("BlogEngine.MobileDevices"),
                      RegexOptions.IgnoreCase | RegexOptions.Compiled);

        /// <summary>
        /// Gets a value indicating whether the client is a mobile device.
        /// </summary>
        /// <value><c>true</c> if this instance is mobile; otherwise, <c>false</c>.</value>
        public static bool IsMobile
        {
            get
            {
                HttpContext context = HttpContext.Current;
                if (context != null)
                {
                    HttpRequest request = context.Request;
                    if (request.Browser.IsMobileDevice)
                        return true;

                    if (!string.IsNullOrEmpty(request.UserAgent) && MobileRegex.IsMatch(request.UserAgent))
                        return true;
                }

                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        public static string GetFormsAuthCookie()
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
// ReSharper disable PossibleNullReferenceException
                return HttpContext.Current.Request.Cookies[".ASPXAUTH"].Value;
// ReSharper restore PossibleNullReferenceException
            }
            throw new BizException("Anonymous access");
        }

        public static string GetUserObjectCacheKey()
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                return HttpContext.Current.User.Identity.Name.ToLower() + "_" + GetFormsAuthCookie();
            }
            throw new BizException("Anonymous access");
            //return string.Empty;
        }

        public static string GetAnanymUserObjectCacheKey()
        {
            return "ananymous_sitemap";
        }

        public static string GetSiteMapObjectCacheKey()
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                return HttpContext.Current.User.Identity.Name.ToLower() + "_" + "sitemap" + "_" + GetFormsAuthCookie();
                //return HttpContext.Current.Request.Cookies[".ASPXAUTH"].Value;
            }
            throw new BizException("Anonymous access");
        }

        public static string GetSiteMapRolesObjectCacheKey()
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                return HttpContext.Current.User.Identity.Name.ToLower() + "_" + "sitemap_role" + "_" +
                       GetFormsAuthCookie();
                //return HttpContext.Current.Request.Cookies[".ASPXAUTH"].Value;
            }
            throw new BizException("Anonymous access");
        }

        public static string GetUserProfileCacheKeyPrefix()
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                return HttpContext.Current.User.Identity.Name.ToLower() + "_";
                //return HttpContext.Current.Request.Cookies[".ASPXAUTH"].Value;
            }
            throw new BizException("Anonymous access");
            //return string.Empty;
        }

        public static string GetNotificationCacheKey()
        {
            return "notification";
        }

        public static bool CheckIfFormsAuthCookieAlreadyExists(string loginName)
        {
            string authkey = CommonUtils.CnvStr(HttpContext.Current.Cache.Get(loginName.ToLower()));
            //If Null / empty means No cookie exists (meaning no previous session exists)
            // if no null/empty means previous logon session still alive
            return !string.IsNullOrEmpty(authkey);
        }

        public static bool CheckIfRedirectcacheExists(string cacheKey)
        {
            string authkey = CommonUtils.CnvStr(HttpContext.Current.Cache.Get(cacheKey.ToLower()));
            return !string.IsNullOrEmpty(authkey);
        }

        public static bool CheckIfNotificationCacheExists(string cacheKey)
        {
            return CheckIfRedirectcacheExists(cacheKey);
        }

        public static void RemoveCacheByMatchingKey(string loginName)
        {
            IDictionaryEnumerator enumerator = HttpContext.Current.Cache.GetEnumerator();

            while ((enumerator.MoveNext()))
            {
                if (enumerator.Key.ToString().Contains(loginName.ToLower()))
                {
                    HttpContext.Current.Cache.Remove(enumerator.Key.ToString());
                }
            }
        }

       
    }
}